	//
	//  GaoButtonLiiTwo.m
	//
	//  Created by Gikki Ares on 2020/12/8.
	//

#import "JsButtonLiiTwo.h"
#import "JsGeometryManager.h"

@interface JsButtonLiiTwo()
{
	UIEdgeInsets insets_iv;
	UILabel *mlb;
	UIImageView *miv;
	UIImageView *miv_background;
	UIButton * mbtn;
	int mi_setupUiCount;
	
	void(^mblock_position)(void);
	NSMutableArray * marr_vm;
	
}

@end


@implementation JsButtonLiiTwo

#pragma mark LifeCycle
	//initWighFrame时,就确保传入正确的UI数据.
- (instancetype)initWithFrame:(CGRect)frame
{
	self = [super initWithFrame:frame];
	if (self) {
		[self commonInit];
	}
	return self;
}
	//init会调用 initWithFrame所以不再设置.
	//- (instancetype)init
	//{
	//	self = [super init];
	//	if (self) {
	//		[self commonInit];
	//	}
	//	return self;
	//}


- (UIImageView *)imageView {
	if(!miv) {
		miv = [UIImageView new];
		[self addSubview:miv];
	}
	return miv;
}

- (instancetype)initWithCoder:(NSCoder *)coder {
	if(self == [super initWithCoder:coder]) {
		
	}
	return self;
}

/**
 1,drawRect,把函数写到代理里面没有用??
 */
	//- (void)drawRect:(CGRect)rect {
	//	if(_delegate&&[_delegate respondsToSelector:@selector(gaoButtonLiiDrawRectAtContext:withLabelFrame:imageFrame:totalSize:)]) {
	//		CGContextRef context = UIGraphicsGetCurrentContext();
	//		[_delegate gaoButtonLiiDrawRectAtContext:context withLabelFrame:[self.vm labelFrame] imageFrame:[self.vm imageViewFrame] totalSize:self.bounds.size];
	//	}
	//
	//}


- (void)awakeFromNib {
	[super awakeFromNib];
	[self commonInit];
}

- (void)commonInit {
	self.countOfStates = 2;
	self.currentState = 0;
	
	JsButtonLiiVm * vm = [JsButtonLiiVm defaultVm];
	JsButtonLiiVm * selectedVm = [JsButtonLiiVm defaultSelectedVm];
	marr_vm = [NSMutableArray array];
	[marr_vm addObject:vm];
	[marr_vm addObject:selectedVm];
	
		//	if(CGSizeEqualToSize(self.frame.size, CGSizeZero)) {
		//		self.frame = CGRectMake(0, 0,self.vm.size.width,self.vm.size.height);
		//	}
		//	else {
		//		self.vm.size = self.frame.size;
		//	}
	
	mbtn = [[UIButton alloc]init];
	[mbtn addTarget:self  action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
	[self addSubview:mbtn];
	[self setupUi];
}

- (void)setFrame:(CGRect)frame {
	[super setFrame:frame];
		//set了Frame,就以该frame为准
	for (JsButtonLiiVm * vm in marr_vm) {
		vm.size = frame.size;
	}
		//setupUi,不会更改自己的size.
		//当frame改变时,需要自己手动update
	
}

- (void)setBounds:(CGRect)bounds {
	[super setBounds:bounds];
		//	if(self.isSelected) {
		//		self.selectedVm.size = bounds.size;
		//	}
		//	else {
		//		self.vm.size = bounds.size;
		//	}
}



	//自己的frame还是交给父视图去控制.
	//layoutSubview只负责直接子视图的布局.
- (void)layoutSubviews {
	[super layoutSubviews];
	if(mblock_position) {
		mblock_position();
		mblock_position = nil;
	}
}

	//返回第一个状态的vm;
-(JsButtonLiiVm *)unselectedVm {
	return marr_vm[0];
}

- (void)setUnselectedVm:(JsButtonLiiVm *)vm {
	marr_vm[0] = vm;
	[self setupUi];
}

- (JsButtonLiiVm *)selectedVm {
	if(marr_vm.count>=2) {
		return marr_vm[1];
	}
	else {
		return nil;
	}
}

	//
- (void)setupUiWithVm:(JsButtonLiiVm *) vm {
		//	NSLog(@"Update Ui.");
	mi_setupUiCount = 0;
	{
			//设置前景图
		if(vm.foregroundImage) {
			if(!miv) {
				miv = [UIImageView new];
				[self addSubview:miv];
			}
			miv.image = vm.foregroundImage;
			miv.hidden = NO;
		}
		CGRect foregroundImageViewFrame = vm.foregroundImageViewFrame;
		if(CGRectIsNull(foregroundImageViewFrame)) {
			foregroundImageViewFrame = self.bounds;
		}
		miv.frame = foregroundImageViewFrame;
		if(vm.isForegroundImageCircle) {
			CGFloat minSize = MIN(miv.bounds.size.width,miv.bounds.size.height);
			miv.layer.cornerRadius = minSize/2;
			miv.layer.masksToBounds = YES;
		}
	}
	
	{
			//设置label
			//Create if needed
		if(vm.text||vm.atext) {
			if(!mlb) {
				mlb = [UILabel new];
				[self addSubview:mlb];
			}
			
				//设置frame
			CGRect labelFrame = vm.labelFrame;
			if(CGRectIsNull(labelFrame)) {
				labelFrame = self.bounds;
			}
			mlb.frame = labelFrame;
			
			mlb.textColor = vm.labelTextColor;
			mlb.font = vm.labelFont;
			mlb.textAlignment = vm.mTextAlignment;
				//优先显示富文本,没有再显示文本.
			if(vm.atext) {
				mlb.attributedText = vm.atext;
			}
			else {
				mlb.text = vm.text;
			}
		}
	}
	
	self.backgroundColor = vm.backgroundColor;
	self.layer.masksToBounds = NO;
	
	if(vm.isCircle) {
		CGFloat minSize = MIN(self.bounds.size.width,self.bounds.size.height);
		self.layer.cornerRadius = minSize/2;
		self.layer.masksToBounds = YES;
	}
	if(vm.cornerRaidus) {
		self.layer.cornerRadius = vm.cornerRaidus;
		self.layer.masksToBounds = YES;
	}
	
	
	if(vm.borderWidth) {
		self.layer.borderWidth = vm.borderWidth;
		self.layer.borderColor = vm.borderColor.CGColor;
	}
	{
			//btn
		CGRect frame = vm.btnFrame;
		if(CGRectIsNull(frame)) {
			frame = self.bounds;
		}
		mbtn.frame = frame;
	}
	[self bringSubviewToFront:mbtn];
}

#pragma mark 2 BasicFunction

#pragma mark 3 Interface
	//设置背景图片
- (void)setBackgroundImage:(UIImage *)image {
	miv_background.image = image;
	miv_background.hidden = NO;
}

- (void)setTitle:(NSString *)title {
	[self setTitle:title withFont:nil];
}

- (void)setTitle:(NSString *)title withFont:(nullable UIFont * )font {
	[self setTitle:title withFont:font textColor:nil];
}

- (void)setTitle:(NSString *)title withFont:(nullable UIFont *)font textColor:(nullable UIColor *)textColor {
	if(!mlb) {
		mlb = [UILabel new];
		[self addSubview:mlb];
	}
	
	for (JsButtonLiiVm * vm in marr_vm) {
		vm.text = title;
		if(font) {
			vm.labelFont = font;
		}
		if(textColor){
			vm.labelTextColor = textColor;
		}
		vm.atext = nil;
	}
	[self setupUi];
}

- (void)setImage:(UIImage *)image {
	[self setSelectedImage:image unselectedImage:image];
	
}

	//设置图片则显示imageView;
- (void)setSelectedImage:(UIImage *)selectedImage unselectedImage:(UIImage *)unselectedImage {
	self.unselectedVm.foregroundImage = unselectedImage;
	self.selectedVm.foregroundImage = selectedImage;
	miv.hidden = NO;
	[self setupUi];
}

	//1为selected,0位unselectd
- (BOOL)isSelected {
	return self.currentState == 1;
}


- (void)setUnselected {
	self.currentState = 0;
	[self setupUi];
}

- (void)setSelected {
	self.currentState = 1;
	[self setupUi];
}


- (void)toggle {
	if([self isSelected]) {
		[self setUnselected];
	}
	else {
		[self setSelected];
	}
}


	//更新所有的内容,而layoutSubview,只是影响布局
	//没有就创建.所有的
	//先不使用layoutsubviews试试,看.
- (void)setupUi {
	mi_setupUiCount ++;
	JsButtonLiiVm * vm = marr_vm[_currentState];
	
		//当1个runloop有多次更新请求时,只触发一次.
	if(mi_setupUiCount == 1) {
		dispatch_async(dispatch_get_main_queue(), ^{
			[self setupUiWithVm:vm];
		});
	}
}

	- (void)updateFontColor:(UIColor *)color {
		for (JsButtonLiiVm * vm in marr_vm) {
			vm.labelTextColor = color;
		}
		
		[self setupUi];
	}
	
	- (void)updateAttributedText:(NSAttributedString *)astr {
		for (JsButtonLiiVm * vm in marr_vm) {
			vm.atext = astr;
		}
		
		[self setupUi];
	}

#pragma mark 2 interface

- (void)updateLabelAlignment:(NSTextAlignment)textAlignment {
	for (JsButtonLiiVm * vm in marr_vm) {
		vm.mTextAlignment = textAlignment;
	}
	[self setupUi];
}




#pragma mark 5 Event
	//先更新状态,再调用回调.
- (IBAction)onClick:(id)sender {
	[self toggle];
		//调用回调
	if(self.clickCallback) {
		self.clickCallback([self isSelected]);
	}
		//调用代理.
	if(_delegate &&[_delegate respondsToSelector:@selector(jsButtonLiiTwoClicked:)]){
		[_delegate jsButtonLiiTwoClicked:self];
	}
	
}
@end


